package shop;

import shop.ShopCapa.SellGoal;
import jadex.bdiv3.features.IBDIAgentFeature;
import jadex.bdiv3.runtime.ICapability;
import jadex.bridge.service.annotation.Service;
import jadex.bridge.service.annotation.ServiceComponent;
import jadex.commons.future.Future;
import jadex.commons.future.IFuture;

/**
 *  The shop for buying goods at the shop.
 */
@Service
public class ShopService implements IShopService 
{
	//-------- attributes --------
	
	/** The component. */
	@ServiceComponent
	protected ICapability	capa;
	
	/** The shop name. */
	protected String name;
	
	//-------- constructors --------

	/**
	 *  Create a new shop service.
	 */
	public ShopService(String name)
	{
		this.name = name;
	}

	//-------- methods --------
	
	/**
	 *  Get the shop name. 
	 *  @return The name.
	 *  
	 *  @directcall (Is called on caller thread).
	 */
	public String getName()
	{
		return name;
	}
	
	/**
	 *  Buy an item.
	 *  @param item The item. 买商品
	 */
	public IFuture<ItemInfo> buyItem(final String item, final double price)
	{
		ShopCapa shop = (ShopCapa)capa.getPojoCapability();
		SellGoal sell = shop.new SellGoal(item, price);
		return capa.getAgent().getComponentFeature(IBDIAgentFeature.class).dispatchTopLevelGoal(sell);
	}
	
	/**
	 *  Get the item catalog.
	 *  @return  The catalog.
	 */	
	public IFuture<ItemInfo[]> getCatalog()
	{
		final Future<ItemInfo[]> ret = new Future<ItemInfo[]>();
		ShopCapa shop = (ShopCapa)capa.getPojoCapability();
		
		////////////////////////////////////////////////////////////
		System.out.println("买后数量改变  反回数组 从新获取目录 可以在这里设置数据" + shop.getShopname());
		java.util.List<ItemInfo> catalog =new shop.service.Service().getShopItemInfoByShopName(shop.getShopname());
		shop.setCatalog(catalog);
		
		// 买后数量改变  反回数组 从新获取目录
		/////////////////////////////////////////////////////////////
		
		
		ret.setResult(shop.getCatalog().toArray(new ItemInfo[shop.getCatalog().size()]));
		
		//java.util.List<ItemInfo> catalog=shop.getCatalog();
		//new shop.service.Service().updateItemQuantityByItemName(catalog);
		
//		for (ItemInfo object : list) {
//			
//			System.out.println("111111111111" + object.getName());
//			System.out.println("222222222222" + object.getQuantity());
//
//		}
		
		System.out.println("ShopService 买后数量改变  反回数组 从新获取目录 ");
		
		return ret;
	}

	/**
	 *  Get the string representation.
	 *  @return The string representation.
	 */
	public String toString()
	{
		return name;
	}
}
